package com.cdn.allmobilestore.widget;

import android.content.Context;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.cdn.allmobilestore.R;
import com.cdn.allmobilestore.activity.BaseActivity;

public class SearchBar extends RelativeLayout {

	EditText txtSearch;
	Button btnDelete;	
	ImageView btnBack;
	Context _context;
	
	public SearchBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	/**Phong
	 * @param context
	 * @param attrs
	 */
	public SearchBar(final Context context, AttributeSet attrs) {
		super(context, attrs);		
		_context = context;
	    LayoutInflater.from(context).inflate(R.layout.search_bar, this, true);
	    this.txtSearch 	= (EditText) findViewById(R.id.txt_search);
	    this.btnBack 	= (ImageView) findViewById(R.id.back_search);
	    this.btnDelete 	= (Button) findViewById(R.id.btn_delete);
	    this.btnDelete.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				txtSearch.setText("");
				InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(getWindowToken(), 0);
			}
		});
	    
	}

	/**Phong
	 * @param context
	 */
	public SearchBar(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public void focusSearchBox()
	{
		InputMethodManager imm = (InputMethodManager) _context.getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
	}
	
	public void setTextHint(CharSequence text)
	{
		this.txtSearch.setHint(text);
	}
	
	public void setText(CharSequence text)
	{
		this.txtSearch.setText(text);
	}
	
	public void addTextChangedListener(TextWatcher textChangedListener){
		this.txtSearch.addTextChangedListener(textChangedListener);
	}
	
	public void enableSearchBack(boolean enable,final BaseActivity activity)
	{
		if(enable)
		{
			this.btnBack.setVisibility(View.VISIBLE);			
			this.btnBack.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					activity.onBackPressed();
				}
			});
			
		}
		    
	}
	
	public void setOnEditorActionListener(EditText.OnEditorActionListener listner)
	{
		this.txtSearch.setOnEditorActionListener(listner);
	}
	
	

}
